package com.zfei.gfb.request;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.util.ObjUtil;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


/**
 * @author fgh
 */
@Data
public class GfbHouseAddRequest {

    // 房源名称
    @ApiModelProperty(value = "房源名称", required = true)
    private String name;
    // 户型
    @ApiModelProperty(value = "户型， 例如2室1厅1卫，传“2-1-1”", required = true)
    private String unitType;

    @ApiModelProperty(hidden = true)
    private Integer unit;
    // 图片url集合,json数组形式
    @ApiModelProperty(value = "图片数组", required = true)
    private List<String> images;
    // 建筑面积
    @ApiModelProperty(value = "建筑面积", required = true)
    private Float area;
    // 朝向
    @ApiModelProperty(value = "朝向， 北0南1西2东3", required = true)
    private Integer face;
    // 联系人电话
    @ApiModelProperty(value = "联系人电话", required = true)
    private String phone;
    // 月租金
    @ApiModelProperty(value = "月租金", required = true)
    private Integer price;
    // 出租方式
    @ApiModelProperty(value = "出租方式：2:长租 1:短租", required = true)
    private Integer type;
    // 房源地图坐标
    @ApiModelProperty(value = "坐标", required = true)
    private String point;
    // 房源地址
    @ApiModelProperty(value = "详细地址", required = true)
    private String address;
    // 房源描述
    @ApiModelProperty(value = "房源描述", required = true)
    private String content;

    @ApiModelProperty(value = "村民用户id", required = true)
    private Integer userId;


    public void checkInsertNull() {
        if (ObjUtil.anyNull(name, unitType, images, area, face, phone, price, point, address, content, type)) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (StringUtils.isAnyBlank(name, phone, address, content)) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (CollectionUtil.isEmpty(images)) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (name.length() > Const.NAME_LENGTH) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (unitType.split("-").length != 3) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (area <= 0) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (area < Const.HOUSE_AREA && type != Const.SHORT_RENTALS) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (area >= Const.HOUSE_AREA && type != Const.LONG_RENTALS) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (!Const.legalFace(face)) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
//        if(!Validator.isMobile(phone)){
//            throw new StatusException(Status.INVALID_ARGUMENT);
//        }
        if (price <= 0) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (content.length() > Const.CONTENT_LENGTH) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if (images.size() > Const.IMAGE_SIZE) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        for (String image : images) {
            if (!Validator.isUrl(image)) {
                throw new StatusException(Status.INVALID_ARGUMENT);
            }
        }
        try {
            unit = Integer.valueOf(unitType.split("-")[0]);
        } catch (Exception e) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
    }
}

